
function initialArray(width, height){
  const result = [];
  for(let i=0;i<width; i++){
    result[i] = [];
    for(let j=0;j<height;j++){
      result[i][j] = 0;
    }
  }
  return result;
}

function initMap(tiledMap){
  //TODO: Start with a 2 dimension array in which all elements are zero, for each layer iterate all tileds in it, if the tile is a barrier, set the according element in the array to 1.
  const map = initialArray(tiledMap.width, tiledMap.height);
  //Init barriers 
  demo.tiledMap.customTiles['barrier'].forEach(tile => {
    map[tile.pos.y][tile.pos.x] = 1;
  });
  demo.tiledMap.customTiles['bound'].forEach(tile => {
    map[tile.pos.y][tile.pos.x] = 1;
  });
  //End init barriers
  
  //Init start and goal
  const startPoint = demo.tiledMap.customTiles['start'][0];
  map[startPoint.pos.y][startPoint.pos.x] = 2;
  const goalPoint = demo.tiledMap.customTiles['goal'][0];
  map[goalPoint.pos.y][goalPoint.pos.x] = 3;
  //End Init start and goal

  return map;
}


function start(url){
  if(url == null){
    throw "Please provide an correct url";
  }else{
    return demo(url).then(tiledMap => {
      //Init a discrete array:
      const map = initMap(tiledMap);
      //console.log(map)
      const paths = A_star(map).reverse();
      const g = new PIXI.Graphics();
      g.beginFill(0xDE3249);
      paths.forEach(p => {
        g.drawCircle(p.x * 32 + 16, p.y * 32 + 16, 5, 5);
      });
      g.endFill();
      demo.stage.addChild(g);
      //console.log(paths);
      //return paths;
    }) 
  }
}
